package com.hp.bon.sgw.wh.channel.especial.bizSlot;

import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.outside.BizSlot;
import com.hp.bon.sgw.wh.channel.outside.bizSlotOutQuery.utils.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class RechargeQueryBizSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(RechargeQueryBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        try {


            String bankCode = bizParam.getParamString(BizConsts.KEY_ESPECIAL_TAOBAO_BANKCODE);
            String paymentSerialNbr = bizParam.getParamString(BizConsts.KEY_ESPECIAL_TBORDERNO);
            String bankCodeSet = bizParam.getParamString(BizConsts.KEY_ESPECIAL_BANK_CODE_SET);

            String channelId = "";
            String[] bankCodeSets = bankCodeSet.split(",");
            for (String item : bankCodeSets) {
                if (String.valueOf(item.split("\\|")[0]).equals(bankCode)) {
                    channelId = String.valueOf(item.split("\\|")[1]);
                }
            }


            JSONObject jsonObject = new JSONObject();
            jsonObject.put("channelId", channelId);
            jsonObject.put("bankCode", bankCode);
            jsonObject.put("serialType", "0");
            jsonObject.put("paymentSerialNbr", paymentSerialNbr);
            jsonObject.put("areaCode", "027");

            StringBuilder buildStringMes = new StringBuilder();
            buildStringMes.append(jsonObject.toJSONString());

            //发送请求
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
            String respStr = HttpClientUtils.postHttpStr(buildStringMes.toString(), webserviceUrl);

            if (respStr.equals("")) {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                return bizResult;
            }

            bizResult.setHttpBody(respStr);
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info(e.toString());
        }
        return bizResult;
    }

    public static void jsonToXmlStr(BizParam bizParam, StringBuffer buffer, String accNbr, List<String> list, String startTime, String endTime) {
        buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        buffer.append("<HqCheck>");
        buffer.append("<BillingMode>1</ BillingMode >");
        buffer.append("<MDN>" + accNbr + "</MDN>");
        buffer.append("<ServId>" + bizParam.getParamString("prodInstId") + "</ServId>");
        buffer.append("<AreaCode>" + bizParam.getParamString("regionId") + "</AreaCode>");
        buffer.append("<CkMDNlist>");
        for (int i = 0; i < list.size(); i++) {
            buffer.append("<CkMDN>" + list.get(i) + "</CkMDN>");
        }
        buffer.append("</CkMDNlist>");
        buffer.append("<StartTime>" + startTime + "</StartTime>");
        buffer.append("<EndTime>" + endTime + "</EndTime>");
        buffer.append("</HqCheck>");
    }

}
